<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : score.class.php   FILE_PATH : \\classes\application\score.class.php
 * ....书写本页代码的说明
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Oct 16 18:06:29 CST 2007
 */
class ScoreClass extends FrameWork {
	/**
	 * 添加商品评价
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function addScore($input_param){
		$arr = array();
		$arr['order_id'] = $input_param['orderid'];
		$arr['member_id'] = $input_param['memberid'];
		$arr['grade_member_id'] = $input_param['gradememberid'];
		$arr['score'] = $input_param['score'];
		$arr['genre'] = $input_param['genre'];
		$arr['pubtime'] = time();
		$arr['content'] = Common::replacebr($input_param['content']);
		$arr['score_pay_type'] = $input_param['score_pay_type'];
		if ($this->isExistScore($input_param) == false){
			return $GLOBALS['db']->InsertRow($arr,'score','score_id');
		}else{
			return false;
		}
	}
	
	/**
	 * 删除评价
	 *
	 * @param int $scoreid
	 */
	function deleScore($scoreid){
		if (intval($scoreid) > 0){
			return $GLOBALS['db']->DelRow(intval($scoreid),'score','score_id');
		}else {
			return false;
		}
	}

	/**
	 * 查看评价是否已经存在
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function isExistScore($input_param){
		$check_row = $GLOBALS['db']->GetList("and order_id='" . $input_param['orderid'] . "' and member_id='" . $input_param['memberid'] . "' and grade_member_id='" . $input_param['gradememberid'] . "' and genre='" . $input_param['genre'] . "'",$page,'score','score_id');
		if ($check_row[0]['score_id'] > 0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 修改评价
	 *
	 * @param var[] $input_param
	 * @param string $genre  修改类型，0用户前台修改，1后台修改
	 * @return boolen
	 */
	function updateScore($input_param,$genre="0"){
		if (intval($input_param['score_id']) > 0){
			if ($input_param['genre'] == "s"){
				$subsql = " and member_id='" . $input_param['memberid'] . "'";
			}else{
				$subsql = " and grade_member_id='" . $input_param['gradememberid'] . "'";
			}
			$condition_extend = " score_id='" . $input_param['score_id'] . "' " . $subsql;
			$result = $GLOBALS['db']->GetTheRow($condition,'score');
			if ($result['modify_time'] == "" || $genre="1"){
				//修改评价
				$arr = array();
				$arr['score'] = $input_param['score'];
				$arr['content'] = $input_param['content'];
				$arr['modify_time'] = time();
				$arr['modify_reason'] = $input_param['txtmodireason'];
				return $GLOBALS['db']->UpdateRow($input_param['score_id'],$arr,'score','score_id');
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	/**
	 * 得到评价列表
	 * @author workflow Develop Team     
	 * @param  $condition 构造条件,$obj_page分页方法
	 * @return array
	 */
	function getScore($condition,&$obj_page){
		$subsql = $this->getCondition($condition);
		$score_array = $GLOBALS['db']->GetJoinList($obj_page,array('score','product_sold','member','member as grademember'),"left join",array("score.order_id=product_sold.sp_id","member.member_id=score.member_id","score.grade_member_id=grademember.member_id"),array('*','*','login_name as member_name','login_name as grade_member'),$subsql,0);
		return $score_array;
	}

	/**
	 * 得到买/卖家的好评率
	 *
	 * @param int $memberid 用户ID
	 * @param string $genre 好评率类别
	 * @return string
	 */
	function getScorePercent($memberid,$genre = "b"){
		$total_row = $GLOBALS['db']->CountRows('score',"grade_member_id='" . $memberid . "' and genre='" . $genre . "'");
		$score_row = $GLOBALS['db']->CountRows('score',"grade_member_id='" . $memberid . "' and genre='" . $genre . "' and score='1'");
		if ($total_row['count'] <=0 ){
			return "0.00%";
		}else{
			return round(($score_row/$total_row)*100,2) . "%";
		}
	}

	/**
	 * 得到买/卖家的评价统计
	 *
	 * @param int $memberid
	 * @return var[]
	 */
	function getStatistic($memberid){
		$score_sta = array('1'=>'good','0'=>'mid','-1'=>'bad');
		$score_array = $GLOBALS['db']->GetList(" and grade_member_id = '" . $memberid . "'",$page,'score');
		foreach($score_array as $k=>$v){
			if((time() - $v['pubtime'])/(60*60*24)<=7){
				$score_sta_array[$v['genre']]['week'][$score_sta[$v['score']]]++;
				$score_sta_array[$v['genre']]['week']['total']++;
			}
			if((time() - $v['pubtime'])/(60*60*24)<=30){
				$score_sta_array[$v['genre']]['month'][$score_sta[$v['score']]]++;
				$score_sta_array[$v['genre']]['month']['total']++;
			}
			if((time() - $v['pubtime'])/(60*60*24)<=30*6){
				$score_sta_array[$v['genre']]['halfyear'][$score_sta[$v['score']]]++;
				$score_sta_array[$v['genre']]['halfyear']['total']++;
			}
			if((time() - $v['pubtime'])/(60*60*24)>30*6){
				$score_sta_array[$v['genre']]['year'][$score_sta[$v['score']]]++;
				$score_sta_array[$v['genre']]['year']['total']++;
			}
			$score_sta_array[$v['genre']]['total'][$score_sta[$v['score']]]++;
			$score_sta_array[$v['genre']]['total']['total']++ ;
		}
		return ($score_sta_array);
	}
	
	/**
	 * 得到买/卖家的评价统计
	 *
	 * @param array $arr_score
	 * @return array
	 */
	function getCountUserScore ($arr_score) {
		$arr_sta = array('one_week_good' => 0,'one_month_good' => 0,'six_month_good' => 0,'former_six_month_good' => 0,'one_week_normal' => 0,'one_month_normal' => 0,'six_month_normal' => 0,'former_six_month_normal' => 0,'one_week_bad' => 0,'one_month_bad' => 0,'six_month_bad' => 0,'former_six_month_bad' => 0,'good_count'=>0,'all_count'=>0,'good_percent'=>"0.00%");				
		if (is_array($arr_score)){
			foreach ($arr_score as $k => $v){
				//评价等级
				if ($v['score'] == 1){//好评
					if (time() - $v['pubtime'] <= 7*24*60*60){//最近1周
						$arr_sta['one_week_good']++;
					}
					if (time() - $v['pubtime'] <= 30*24*60*60){//最近1个月
						$arr_sta['one_month_good']++;
					}
					if (time() - $v['pubtime'] <= 6*30*24*60*60){//最近6个月
						$arr_sta['six_month_good']++;
					}
					if (time() - $v['pubtime'] > 6*30*24*60*60){//6个月前
						$arr_sta['former_six_month_good']++;
					}	
					$arr_sta['good_count']++;				
				}
				if ($v['score'] == 0){//中评
					if (time() - $v['pubtime'] <= 7*24*60*60){//最近1周
						$arr_sta['one_week_normal']++;
					}
					if (time() - $v['pubtime'] <= 30*24*60*60){//最近1个月
						$arr_sta['one_month_normal']++;
					}
					if (time() - $v['pubtime'] <= 6*30*24*60*60){//最近6个月
						$arr_sta['six_month_normal']++;
					}
					if (time() - $v['pubtime'] > 6*30*24*60*60){//6个月前
						$arr_sta['former_six_month_normal']++;
					}
				}
				if ($v['score'] == '-1'){//坏评
					if (time() - $v['pubtime'] <= 7*24*60*60){//最近1周
						$arr_sta['one_week_bad']++;
					}
					if (time() - $v['pubtime'] <= 30*24*60*60){//最近1个月
						$arr_sta['one_month_bad']++;
					}
					if (time() - $v['pubtime'] <= 6*30*24*60*60){//最近6个月
						$arr_sta['six_month_bad']++;
					}
					if (time() - $v['pubtime'] > 6*30*24*60*60){//6个月前
						$arr_sta['former_six_month_bad']++;
					}
				}
				$arr_sta['all_count']++;
			}
			//好评率
			if ($arr_sta['all_count'] != 0) {
				$arr_sta['good_percent'] = round(($arr_sta['good_count']/$arr_sta['all_count'])*100,2) . "%";
			}
			return $arr_sta;
		}		
	}	
	

	/**
	 * 获得所有评价
	 *
	 * @param var[] $condition 条件
	 * @param obj $obj_page 分页对象
	 * @return var[]
	 */
	function getAllScore($condition,&$obj_page){
		$subsql = $this->getCondition($condition);
		
		$score_array = $GLOBALS['db']->GetJoinList($obj_page,array('score','product_sold','member','member as grademember'),"left join",array("score.order_id=product_sold.sp_id","member.member_id=score.member_id","score.grade_member_id=grademember.member_id"),array('*','*','login_name as member_name','login_name as grade_member'),$subsql,array(" receive_id desc"));
		
		return $score_array;
		
		
	}

	/**
	 * 组合成为查询条件
	 *
	 * @param var[] $arr
	 * @return string
	 */
	function getCondition($arr){
		$str = "";
		if ($arr['membername'] != ""){
			$str .= " and member.login_name like '%" . $arr["membername"] . "%'";
		}
		if ($arr['grademember'] != ""){
			$str .= " and grademember.login_name like '%" . $arr["grademember"] . "%'";
		}
		if ($arr['score'] != ""){
			$str .= " and score.score ='" . $arr["score"] . "'";
		}
		if ($arr['productname'] != ""){
			$str .= " and product_sold.p_name like '%" . $arr["productname"] . "%'";
		}
		if ($arr['ordercode'] != ""){
			$str .= " and product_sold.sp_code = '" . $arr["ordercode"] . "'";
		}
		if ($arr['genre'] != ""){
			$str .= " and score.genre = '" . $arr["genre"] . "'";
		}
		//评价人ID
		if ($arr['member_id'] != ''){
			$str .= " and score.member_id = '" . $arr['member_id'] . "'";
		}
		//被评价人ID
		if ($arr['grade_member_id'] != ''){
			$str .= " and score.grade_member_id = '" . $arr['grade_member_id'] . "'";
		}
		//支付类型
		if ($arr['score_pay_type'] != '' && $arr['score_pay_type'] != '0'){
			$str .= " and score.score_pay_type = '" . $arr['score_pay_type'] . "'";
		}
		if ($arr['time'] != '') {
			$operator = '>=';
			switch ($arr['time']) {
				case 'week'://最近1周
					$time = time()- 7*24*60*60;
					break;
				case 'month'://最近1周
					$time = time()- 30*24*60*60;
					break;	
				case 'six_month'://最近6个月
					$time = time()- 6*30*24*60*60;
					break;	
				case 'former_six_month'://6个月前
					$time = time()- 6*30*24*60*60;
					$operator = '<';
					break;														
			}			
			$str .= " and score.pubtime $operator '" . $time . "'";			
		}
		//排序
		if ($arr['order_by'] != ''){
			$str .= " order by " . $arr['order_by'] . "";
		}
		return $str;
	}

	/**
	 * 得到某条评价的内容
	 *
	 * @param int $scoreid
	 * @return var[]
	 */
	function getOneScore($scoreid){
		if (intval($scoreid) > 0){
			return $GLOBALS['db']->GetOneRow($scoreid,'score','score_id');
		}else{
			return false;
		}
	}
}
?>